package com.ickd.configuration;

import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.common.constant.CommonConstant;
import cloud.tianai.captcha.resource.ResourceStore;
import cloud.tianai.captcha.resource.common.model.dto.Resource;
import cloud.tianai.captcha.resource.common.model.dto.ResourceMap;
import cloud.tianai.mate.captcha.common.constant.MateCaptchaTypeConstant;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static cloud.tianai.captcha.common.constant.CommonConstant.DEFAULT_TAG;
import static cloud.tianai.mate.captcha.generator.impl.MateWordOrderClickImageCaptchaGenerator.wordToResourceMap;

/**
 * @Author: 天爱有情
 * @date 2022/7/11 14:22
 * @Description 负责模板和背景图存储的地方
 */
@Component
@RequiredArgsConstructor
public class ResourceConfiguration implements InitializingBean {
    @Autowired
    private SysConfig sysConfig;
    // 该ResourceStore 由 tianai-captcha-springboot-starter 自动装配
    private final ResourceStore resourceStore;


    @Override
    public void afterPropertiesSet() throws Exception {

        resourceStore.clearAllResources();
        resourceStore.clearAllTemplates();
        String type = sysConfig.getResourceType();

        // 滑块验证码 模板
        ResourceMap template1 = new ResourceMap(4);
        template1.put("active.png", new Resource(type, sysConfig.getResourcePath() + "template/1/active.png"));
        template1.put("fixed.png", new Resource(type, sysConfig.getResourcePath() + "template/1/fixed.png"));
        ResourceMap template2 = new ResourceMap(4);
        template2.put("active.png", new Resource(type, sysConfig.getResourcePath() + "template/2/active.png"));
        template2.put("fixed.png", new Resource(type, sysConfig.getResourcePath() + "template/2/fixed.png"));
        // 旋转验证码 模板
        ResourceMap template3 = new ResourceMap(4);
        template3.put("active.png", new Resource(type, sysConfig.getResourcePath() + "template/3/active.png"));
        template3.put("fixed.png", new Resource(type, sysConfig.getResourcePath() + "template/3/fixed.png"));
        // 旋转验证码 带有混淆功能的混淆模板
        ResourceMap template4 = new ResourceMap("obfuscate", 4);
        template4.put("active.png", new Resource(type, sysConfig.getResourcePath() + "template/4/active.png"));
        template4.put("fixed.png", new Resource(type, sysConfig.getResourcePath() + "template/4/fixed.png"));
        template4.put("mask.png", new Resource(type, sysConfig.getResourcePath() + "template/4/mask.png"));
        resourceStore.addTemplate(CaptchaTypeConstant.ROTATE, template4);

        // 1. 添加一些模板
        resourceStore.addTemplate(CaptchaTypeConstant.SLIDER, template1);
        resourceStore.addTemplate(CaptchaTypeConstant.SLIDER, template2);
        resourceStore.addTemplate(CaptchaTypeConstant.ROTATE, template3);
        resourceStore.addTemplate(MateCaptchaTypeConstant.SLIDER2, template1);
        resourceStore.addTemplate(MateCaptchaTypeConstant.SLIDER2, template2);

        // (语序点选成语字典，这里只演示一个)
        resourceStore.addTemplate(MateCaptchaTypeConstant.WORD_ORDER_IMAGE_CLICK, wordToResourceMap("鸡你太美", "default"));

        // 添加icon(图标点选)
        resourceStore.addResource(CommonConstant.IMAGE_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "icon/1.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.IMAGE_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "icon/2.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.IMAGE_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "icon/3.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.IMAGE_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "icon/4.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.IMAGE_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "icon/5.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.IMAGE_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "icon/6.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.IMAGE_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "icon/7.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.IMAGE_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "icon/8.png", DEFAULT_TAG));


        // 点选icon(蜂窝图标)
        resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "honeycomb/mask.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "honeycomb/mask2.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "honeycomb/mask3.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "honeycomb/mask4.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "honeycomb/mask5.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "honeycomb/mask6.png", DEFAULT_TAG));
        resourceStore.addResource(CommonConstant.HONEYCOMB_CLICK_ICON, new Resource(type, sysConfig.getResourcePath() + "honeycomb/mask7.png", DEFAULT_TAG));
        // 刮刮卡
        resourceStore.addResource(CommonConstant.SCRAPE_ICON, new Resource(type, sysConfig.getResourcePath() + "scrape-icon/包包.png", null, "包包"));
        resourceStore.addResource(CommonConstant.SCRAPE_ICON, new Resource(type, sysConfig.getResourcePath() + "scrape-icon/小猫咪.png", null, "小猫咪"));
        resourceStore.addResource(CommonConstant.SCRAPE_ICON, new Resource(type, sysConfig.getResourcePath() + "scrape-icon/篮球.png", null, "篮球"));
        resourceStore.addResource(CommonConstant.SCRAPE_ICON, new Resource(type, sysConfig.getResourcePath() + "scrape-icon/香蕉.png", null, "香蕉"));

        resourceStore.addResource(MateCaptchaTypeConstant.SCRAPE, new Resource(type, sysConfig.getResourcePath() + "bgimages/a.jpg"));

        // 2. 添加自定义背景图片
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/a.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/b.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/c.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/d.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/e.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/f.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/g.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/h.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/i.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/j.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/k.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/l.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/m.jpg"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(type, sysConfig.getResourcePath() + "bgimages/n.jpg"));


        resourceStore.addResource(CaptchaTypeConstant.ROTATE, new Resource(type, sysConfig.getResourcePath() + "bgimages/48.jpg"));

        resourceStore.addResource(CaptchaTypeConstant.CONCAT, new Resource(type, sysConfig.getResourcePath() + "bgimages/48.jpg"));

        resourceStore.addResource(MateCaptchaTypeConstant.ROTATE_DEGREE, new Resource(type, sysConfig.getResourcePath() + "bgimages/135.png"));
        resourceStore.addResource(MateCaptchaTypeConstant.ROTATE_DEGREE, new Resource(type, sysConfig.getResourcePath() + "bgimages/139.png"));
        resourceStore.addResource(MateCaptchaTypeConstant.ROTATE_DEGREE, new Resource(type, sysConfig.getResourcePath() + "bgimages/141.png"));

        resourceStore.addResource(CaptchaTypeConstant.WORD_IMAGE_CLICK, new Resource(type, sysConfig.getResourcePath() + "bgimages/c.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.IMAGE_CLICK, new Resource(type, sysConfig.getResourcePath() + "bgimages/a.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.HONEYCOMB_IMAGE_CLICK, new Resource(type, sysConfig.getResourcePath() + "bgimages/a.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.WORD_ORDER_IMAGE_CLICK, new Resource(type, sysConfig.getResourcePath() + "bgimages/48.jpg"));

        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/a.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/b.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/c.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/d.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/e.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/f.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/g.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/h.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/i.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/j.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/k.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/m.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.CURVE, new Resource(type, sysConfig.getResourcePath() + "bgimages/n.jpg"));



        resourceStore.addResource(MateCaptchaTypeConstant.SLIDER2, new Resource(type, sysConfig.getResourcePath() + "bgimages/b.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.SLIDER2, new Resource(type, sysConfig.getResourcePath() + "bgimages/c.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.SLIDER2, new Resource(type, sysConfig.getResourcePath() + "bgimages/d.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.SLIDER2, new Resource(type, sysConfig.getResourcePath() + "bgimages/e.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.SLIDER2, new Resource(type, sysConfig.getResourcePath() + "bgimages/f.jpg"));
        resourceStore.addResource(MateCaptchaTypeConstant.SLIDER2, new Resource(type, sysConfig.getResourcePath() + "bgimages/g.jpg"));
    }
}
